#include "../MsgServer.h"
#include "../MsgRequest.h"
#include "../MsgPublish.h"
#include "jsoncpp/json/json.h"
#include "jsoncpp/json/writer.h"
#include <thread>
#include <chrono>
#include <iostream>

using namespace RobotControl::Common;

void repCallback(zmq::message_t &req, zmq::message_t &rep)
{
	std::cout << "reply recv data";
}

void sub1Callback(zmq::message_t &data)
{
    std::cout << "subscriber 1 recv data";
}

void threadService(zmq::context_t *context)
{
	MsgServer m_server(context);
	m_server.addReply("tcp://0.0.0.0:19000", std::bind(repCallback, std::placeholders::_1, std::placeholders::_2));
	m_server.start();
	while(true){
		m_server.run();
	}
}	


void threadPublish(zmq::context_t *context)
{
	MsgPublish pub(context);
	pub.config("tcp://0.0.0.0:19001");

	while(true){
        {
			static int y = 0;
			Json::Value root;
			root["x"] = 0;
			root["y"] = y++;
			Json::FastWriter write;
			std::string root_str = write.write(root);
            zmq::message_t pub_data(root_str.c_str(), root_str.size());

            pub.publish(pub_data);
            std::cout << "done publish " << y << "\n";
        }
		std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	}
}

// 测试消息服务
int main()
{
	zmq::context_t context(1);
	std::thread t0(threadService, &context);
	std::thread t1(threadPublish, &context);
	t0.join();
	t1.join();
	return 0;
}

